package com.api.projectY.controller;

import com.api.common.core.controller.BaseController;
import com.api.common.core.domain.AjaxResult;
import com.api.common.core.page.TableDataInfo;
import com.api.common.utils.MessageUtils;
import com.api.core.domain.vo.IpcVo;
import com.api.core.service.IiPcService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ipc")
public class IpcController extends BaseController {
    @Autowired
    IiPcService iPcService;

    @PostMapping("/create")
    public AjaxResult create(@RequestHeader("App-Language") String appLang,
                             @RequestBody IpcVo vo) {
        vo.initControl(getLoginUser().getUser().getUserName(), appLang);
        try {
            return AjaxResult.success(iPcService.create(vo));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/modify")
    public AjaxResult modify(@RequestHeader("App-Language") String appLang,
                             @RequestBody IpcVo vo) {
        vo.initControl(getLoginUser().getUser().getUserName(), appLang);
        try {
            iPcService.modify(vo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/remove")
    public AjaxResult delete(@RequestHeader("App-Language") String appLang,
                             @RequestBody IpcVo vo) {
//        if (ObjectUtils.isEmpty(vo.getIpcId()))
//            AjaxResult.warn(MessageUtils.message(appLang, "ipc.noId"));
        if (ObjectUtils.isEmpty(vo.getIpcCode()))
            AjaxResult.warn(MessageUtils.message(appLang, "ipc.code"));
        try {
            iPcService.delete(vo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/item")
    public AjaxResult getItem(@RequestHeader("App-Language") String appLang,
                              @RequestBody IpcVo vo) {
        return AjaxResult.success(iPcService.search(vo));
    }

    @PostMapping("/list")
    public TableDataInfo getList(@RequestHeader("App-Language") String appLang,
                                 @RequestBody IpcVo vo) {
        if(vo.getPageNum()>0){
            startPageByPost(vo.getPageNum(), vo.getPageSize());
        }
        vo.initControl(getLoginUser().getUser().getUserName(), appLang);
        return getDataTable(iPcService.searchList(vo));
    }

    @PostMapping("/tree")
    public TableDataInfo tree(@RequestHeader("App-Language") String appLang,
                              @RequestBody IpcVo vo) {
        return getDataTable(iPcService.tree(vo));
    }
}
